package com.xxrl.shop.domain;

import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.hibernate.annotations.Proxy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

/**
 * @author mis
 */
@ApiModel(description = "Registered user.")
@Entity
@Data
@Proxy(lazy = false)
@NoArgsConstructor
@Table(name = "sp_user")
@EntityListeners(AuditingEntityListener.class)
public class User implements Serializable {
    private static final long serialVersionUID = 1L;

    @ApiModelProperty(notes = "Unique identifier of the User.", example = "1",
            required = true)
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(nullable = false, length = 20)
    private String name;

    @Column(nullable = false, length = 50, unique = true)
    private String email;

    @ApiModelProperty(notes = "User phone number", example = "18000000000")
    @Column(nullable = false, length = 50, unique = true)
    private String phone;

    @ApiModelProperty(notes = "Encrypted password", example = "xxx")
    private String password;
    @ApiModelProperty(notes = "User permission role", example = "user")
    private String role;
    @ApiModelProperty(notes = "Avatar url")
    private String avatar;
    @ApiModelProperty(notes = "Merchant flag")
    private Boolean isMerchant;

    @OneToOne(mappedBy = "user",fetch = FetchType.LAZY, cascade = CascadeType.REMOVE)
    @ToString.Exclude
    @EqualsAndHashCode.Exclude
    @JSONField(serialize = false)
    @JsonIgnore
    private UserInfo userInfo;

    @ApiModelProperty(hidden = true)
    @OneToMany(mappedBy = "user", fetch = FetchType.LAZY)
    @ToString.Exclude
    @EqualsAndHashCode.Exclude
    @JSONField(serialize = false)
    @JsonIgnore
    private Set<Order> orders = new HashSet<>();

    @CreatedDate
    private Long createTime;

    @LastModifiedDate
    private Long lastModifiedTime;
    
    public User(Long id) {
        this.id = id;
    }
}
